<?php
namespace app\shop\controller;

use \think\Controller;
use \think\Db;
class My extends Base
{

    /**
     * 我的信息（即店铺信息）
     * name,avatar
     */
    public function myInfo(){
		extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
       $data = Db::table('up_shops')->where('s_id', $user_id)->find();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 修改头像或者昵称
     * name,avatar
     */
    public function updateInfo(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $update = array();
        if(isset($name)){
            $update['s_name'] = $name;
        }
        if(isset($thumb)){
            $update['s_thumb'] = $thumb;
        }
        if(isset($notice)){
            $update['s_notice'] = $notice;
        }
        if(isset($open)){
            $update['s_open'] = $open;
        }
        if(isset($close)){
            $update['s_close'] = $close;
        }
        //关于免邮、起送、邮费的设置
        if(isset($free)){
            $update['s_free'] = $free;
        }
        if(isset($post)){
            $update['s_post'] = $post;
        }
        if(isset($mianyou)){
            $update['s_mianyou'] = $mianyou;
        }
        if(isset($status)){
            $shop = Db::table('up_shops')
                ->where('s_id', $user_id)
                ->field('s_integral,s_clicktime')
                ->find();
            $jifen = $shop['s_integral'];
            if($jifen < 0){
                $status = 2;
            }else{
                $nowtime = time();
                $lasttime = strtotime($shop['s_clicktime']);
                $value = ($nowtime-$lasttime)/3600;//相差多少小时
                if($value >= 4){
                    $today = date('Y-m-d H:i:s');
                    $update['s_clicktime'] = $today;//传
                }
            }
            $update['s_status'] = $status;//传
        }
        Db::table('up_shops')
            ->where('s_id', $user_id)
            ->update($update);
        return json_encode(['status'=>200,'msg'=>'修改成功']);
    }
    /**
     *
     * 我的银行卡:myAccount
     */
    public function myAccount(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = array();
        $data['acc_shopid'] = $user_id;
        $res = Db::table('up_shop_account')->where($data)->select();
        return json_encode(['status'=>200,'msg'=>$res]);
    }
    /**
     *
     * 添加银行卡
     */
    public function addAccount(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
		if(isset($pass)){
			$right = httpGetCheckAccName($number,urlencode($name));
		}else{
			$right = httpGetCheckAccName($number,$name);
		}
        if($right['result']['res'] != 1){
            return json_encode(['status'=>0,'msg'=>'卡号和姓名不匹配']);
        }
        $data = array();
        $data['acc_shopid'] = $user_id;
        $data['acc_username'] = $name;
        $data['acc_idcard'] = $idcard;
        $data['acc_bank'] = $bank;
        $data['acc_number'] = $number;
        $res = Db::table('up_shop_account')->insert($data);
        if($res){
            return json_encode(['status'=>200,'msg'=>'提交成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'提交失败']);
        }
    }
    /**
     *
     * 编辑银行卡
     */
    public function editAccount(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $right = httpGetCheckAccName($number,$name);
        if($right['result']['res'] != 1){
            return json_encode(['status'=>0,'msg'=>'卡号和姓名不匹配']);
        }
        $data = array();
        $data['acc_username'] = $name;
        $data['acc_idcard'] = $idcard;
        $data['acc_bank'] = $bank;
        $data['acc_number'] = $number;
        $res = Db::table('up_shop_account')->where('acc_id',$acc_id)->update($data);
        if($res){
            return json_encode(['status'=>200,'msg'=>'成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }
    /**
     *
     * 删除银行卡
     */
    public function delAccount(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $res = Db::table('up_shop_account')->where('acc_id',$acc_id)->delete();
        if($res){
            return json_encode(['status'=>200,'msg'=>'成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }

    /**
     * 我的提现申请:askGetMoney
     * $money,$accid
     */
    public function askGetMoney(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $shop = Db::table('up_shops')->where('s_id',$user_id)
            ->field('s_money,s_status')->find();
        if($shop['s_money'] < $money){
            return json_encode(['status'=>0,'msg'=>'提现金额过大，余额不足！']);
        }
        if($shop['s_status'] == 2){
            return json_encode(['status'=>0,'msg'=>'信用太低，不可提现！']);
        }
        if($money < 600){
            return json_encode(['status'=>0,'msg'=>'提现金额不能低于600元']);
        }
        //----------------验证码--------------
        $re = newShopPhonecodeRight($phone,$code);
        if($re['status'] != 200){
            return json_encode(['status'=>0,'msg'=>$re['msg']]);
        }
        //-----------验证码end-------------
        $lessmoney = getMoneyLess($money);
        Db::startTrans();
        try {
            $data = array();
            $data['r_user_id'] = $user_id;
            $data['r_money'] = $money-$lessmoney;
            $data['r_all_money'] = $money;
            $data['r_less_money'] = $lessmoney;
            $data['r_time'] = date('Y-m-d H:i:s');
            $data['r_water'] = getMillisecond() . rand(100000,999999);
            $data['r_type'] = 1;
            $data['r_where'] = $where;
            if($where == 1){
                $data['r_acc_id'] = $accid;
                $acc = Db::table('up_shop_account')->where('acc_id',$accid)->find();
                $data['r_acc_bank'] = $acc['acc_bank'];
                $data['r_acc_username'] = $acc['acc_username'];
                $data['r_acc_num'] = $acc['acc_number'];
            }elseif ($where == 2){
                $data['r_acc_bank'] = '支付宝';
                $data['r_acc_username'] = $name;
                $data['r_acc_num'] = $phone;
            }else{
                $data['r_acc_bank'] = '微信';
                Db::rollback();
                return json_encode(['status'=>0,'msg'=>'暂未开放，敬请期待']);
            }
            Db::table('up_work_jilu')->insert($data);
            Db::table('up_shops')->where('s_id',$user_id)->setDec('s_money',$money);
            Db::commit();
            Db::table('up_shop_phonecode')->where('phone',$phone)->update(['status'=>1]);
            //入消息和推送
            allInNotice('提现申请','您刚刚发起了'.$money.'元的提现，预计会在两小时内到账（节假日除外）',$user_id);
            sendJpush("$user_id", '您刚刚发起了'.$money.'元的提现，预计会在两小时内到账（节假日除外）', array('a' => 'b'), 2, 2, 2);
            return json_encode(['status'=>200,'msg'=>'申请已提交']);
        } catch (\Exception $e) {
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'操作失败']);
        }
    }
    /**
     * 我的提现记录:myJilu
     * page
     */
    public function myJilu(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_work_jilu')
            ->where('r_user_id',$user_id)
//            ->where('r_status',2)
            ->order('r_time DESC')
            ->page($page,10)
            ->select();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 我的提现详情：myJiluInfo
     * r_id
     */
    public function myJiluInfo(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_work_jilu')
            ->where('r_id',$r_id)
            ->find();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 账户对账：myZhanghu
     *
     */
    public function myZhanghu(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
//        $user_id = 1;
//        $page = 1;
        $orderIn = Db::table('up_order')
            ->where('order_status','>',1)
            ->where('order_shopid',$user_id)
            ->where('order_in_out','<>',3)
            ->whereTime('order_paytime','today')
            ->field('count(*) as inNum,SUM(order_user_pay) as inMoney')
            ->find();
        $orderOut = Db::table('up_order')
            ->where('order_status','>',1)
            ->where('order_shopid',$user_id)
            ->where('order_in_out',3)
            ->whereTime('order_paytime','today')
            ->field('count(*) as outNum,SUM(order_user_pay) as outMoney')
            ->find();
        $order = array_merge($orderIn,$orderOut);
        $order['money'] = Db::table('up_shops')
            ->where('s_id',$user_id)
            ->value('s_money');
        //------------------
        $yye = Db::table('up_order')
            ->where('order_status','>',1)
            ->where('order_shopid',$user_id)
            ->field('order_paydate,order_paytime')
            ->order('order_paydate DESC')
            ->group('order_paydate')
            ->page($page,15)
            ->select();
        if($yye){
            foreach ($yye as $k=>$v){
                $in = Db::table('up_order')
                    ->where('order_status','>',1)
                    ->where('order_shopid',$user_id)
                    ->where('order_in_out','<>',3)
                    ->where('order_paydate',$v['order_paydate'])
                    ->field('SUM(order_user_pay) as inMoney')
                    ->find();
                $out = Db::table('up_order')
                    ->where('order_status','>',1)
                    ->where('order_shopid',$user_id)
                    ->where('order_in_out',3)
                    ->where('order_paydate',$v['order_paydate'])
                    ->field('SUM(order_user_pay) as outMoney')
                    ->find();
                if($in){
                    $yye[$k]['money'] = $in['inMoney'];
                }else{
                    $yye[$k]['money'] = 0;
                }
                if($out){
                    $yye[$k]['money'] = $in['inMoney']-$out['outMoney'];
                }

            }
        }
        return json_encode(['status'=>200,'order'=>$order,'yye'=>$yye]);
    }

    /**
     * 某天的营业额:myGetMoney
     *
     */
    public function myGetMoney(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $data = Db::table('up_order')
            ->where('order_status','>',1)
            ->where('order_shopid',$user_id)
            ->where('order_paydate',$day)
            ->field('order_number,order_paytime,order_in_out,order_user_pay')
            ->select();
        $in = Db::table('up_order')
            ->where('order_status','>',1)
            ->where('order_shopid',$user_id)
            ->where('order_in_out','<>',3)
            ->where('order_paydate',$day)
            ->field('SUM(order_user_pay) as inMoney')
            ->find();
        $out = Db::table('up_order')
            ->where('order_status','>',1)
            ->where('order_shopid',$user_id)
            ->where('order_in_out',3)
            ->where('order_paydate',$day)
            ->field('SUM(order_user_pay) as outMoney')
            ->find();
        if($in){
            $money = $in['inMoney'];
        }else{
            $money = 0;
        }
        if($out){
            $money = $in['inMoney']-$out['outMoney'];
        }
        return json_encode(['status'=>200,'data'=>$data,'money'=>$money]);
    }
    /**
     * 我的评分：myjifen
     * type为0是所有，1是加，2是减
     * page
     */
    public function myjifen(){
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $returnData['status'] = 301;
            $returnData['msg'] = "Token失效！请重新登录！";
            return json_encode($returnData);
        }
        $where = array();
        $where['ji_user_id'] = $user_id;
        if(isset($type) && $type>0){
            $where['ji_type'] = $type;
        }
        $data = Db::table('up_jifen')
            ->where($where)
            ->page($page,15)
            ->select();
        $jifen = Db::table('up_shops')
            ->where('s_id',$user_id)
            ->value('s_integral');
        return json_encode(['status'=>200,'msg'=>$data,'jifen'=>$jifen]);
    }
    /**
     * 积分规则：jifen
     *
     */
    public function jifen(){
        $data = Db::table('up_about')->where('a_id',3)->find();
        return json_encode(['status'=>200,'msg'=>$data]);
    }
}